<?php

namespace App\Services\Callback\Payment;

use App\Exceptions\PaymentException;
use App\Library\Payment\Wechat\WechatPayApi;
use App\Models\Applet\Other\PaymentOrderModel;
use App\Services\Applet\Order\ShopOrderService;
use App\Services\Applet\Order\SponsorOrderService;
use App\Services\Callback\BaseCallbackService;
use Exception;

class BasePaymentService extends BaseCallbackService
{
    protected WechatPayApi $wechatPayApi;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        $this->wechatPayApi = loadLibrary('Payment\Wechat\WechatPayApi');
    }

    /**
     * 获取订单服务类
     *
     * @param int $orderType
     * @return SponsorOrderService|ShopOrderService
     * @throws PaymentException
     * @throws Exception
     */
    protected function getOrderService(int $orderType)
    {
        switch ($orderType) {
            case PaymentOrderModel::ORDER_TYPE_SPONSOR:
                return loadService('Applet\Order\SponsorOrderService');
            case PaymentOrderModel::ORDER_TYPE_SHOP:
                return loadService('Applet\Order\ShopOrderService');
            default:
                throw new PaymentException('订单类型错误', PaymentException::CODE_1001);
        }
    }
}
